<?php
namespace app\model;


class Scan {


    static function get($openid){
        global $_G;
        if(empty($openid)){
            $openid = $_G['openid'];
        }
        $params = array();
        $params[':openid'] = $openid;
        $fans = db_find("SELECT * FROM ".tablename(t_scan)."
			WHERE openid = :openid",$params);
        return $fans;
    }

    static function getByThemeId($themeid,$openid){
        global $_G;
        if(empty($themeid) || empty($openid)){
            return;
        }
        $params = array();
        $params[':openid'] = $openid;
        $params[':themeid'] = $themeid;
        $fans = db_find("SELECT * FROM ".tablename(t_scan)."
			WHERE openid = :openid and themeid = :themeid",$params);
        return $fans;
    }

    static function getIconArray($config,$themeid,$openid){

        $lightsum = Scan::getNumByThemeIdAndOpenidAndStatus($themeid,$openid,1);;
        $lightsum = intval($lightsum/$config['iconnum2']);
        if($lightsum >= $config['iconnum']){
            $lightsum = $config['iconnum'];
        }
        $lightlist = array();
        for($i=1;$i<=$lightsum;$i++){
            $lightlist[$i]=$i;
        }
        $darksum = $config['iconnum'] ;
        $darklist = array();
        for($i=$lightsum+1;$i<=$darksum;$i++){
            $darklist[$i]=$i;
        }
        $data = array();
        $data['lightsum']  = $lightsum;
        $data['lightlist'] = $lightlist;
        $data['darksum']   = $darksum;
        $data['darklist']  = $darklist;
        return $data;
    }

    static function getFpenidTodayNum($themeid,$fopenid){
        global $_G, $_P;
        $sql = "SELECT COUNT(*) FROM ".tablename(t_scan)."
    				WHERE  to_days(from_unixtime(addtime)) = to_days(now()) 
    				        and themeid = :themeid 
    				        and fopenid = :openid 
    				        and status = 1";
        $lightsum = intval(db_fetchcolumn($sql,array(":themeid"=>$themeid,":openid"=>$fopenid)));
        return $lightsum;
    }

    static function getFpenidTodayNumXCX($themeid,$uid,$status=0) {
        if (empty($themeid) || empty($uid)) {
            return false;
        }
        $sql = "SELECT * FROM ".tablename(t_scan)."
    				WHERE  to_days(from_unixtime(addtime)) = to_days(now()) 
    				        and themeid = :themeid 
    				        and fromuid = :uid 
    				        and status = 1";
        $list = db_select($sql,array(":themeid"=>$themeid,":uid"=>$uid));


//        $sql = "SELECT * FROM ".tablename(t_scan)."
//                    WHERE themeid = :themeid and fromuid = :uid and status =1";
//        $list = db_select($sql, array(":themeid"=>$themeid,":uid"=>$uid));
        $avatarlist=array();
        foreach ($list as $key => $row) {
            $fans = Fans::getById($row['uid'],$themeid);
            $avatar = array('avatar'=>$fans['avatar']);
            if($status==1) {
                if ($fans['nickname']<>'' || $fans['avatar']<>''){
                    array_push($avatarlist,$avatar);
                }
            }elseif($status==2) {
                if ($fans['mobile']<>''){
                    array_push($avatarlist,$avatar);
                }
            }elseif($status==3) {
                if ($fans['mobile']<>'' && ($fans['nickname']<>'' || $fans['avatar']<>'')){
                    array_push($avatarlist,$avatar);
                }
            }else{
                array_push($avatarlist,$avatar);
            }

        }
        $result = count($avatarlist);
        return $result;
    }

    static function getRealNum($uniacid,$themeid,$openid){
        $sql = "select count(*) from (SELECT COUNT(*) FROM ".tablename(t_scan)."
    				WHERE uniacid = :uniacid and themeid = :themeid and fopenid = :openid and status = 1 group by openid) a";
        $lightsum = intval(db_fetchcolumn($sql,array(":uniacid"=>$uniacid,":themeid"=>$themeid,":openid"=>$openid)));
        return $lightsum;
    }

    static function getShowList($themeid,$shownum){
        global $_G, $_P;
        // $cachekey = 'show'.$_G['mpid'].MODULE_NAME.$themeid;
        // $settings = cache_load($cachekey);
        // if(empty($settings)){
            $p =  array(":themeid"=>$themeid);
            $settings = db_select("SELECT * FROM ".tablename(t_scan)." as f
				WHERE themeid =:themeid and status != 0 group by openid order by id desc limit ".$shownum,$p);
        //     if(!empty($settings)){
        //         cache_write($cachekey, $settings);
        //     }
        // }
        return $settings;
    }

    static function getList($openid,$limit=3){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT g.*,s.title as shop,s.address FROM " . tablename(t_scan) . " as c
                        left join " . tablename(t_goods) . " as g on g.id = c.goodsid
                        left join " . tablename(t_shop) . " as s on g.shopid = s.id
                            where c.status = 1 and g.status = 1 and s.status = 1 and c.openid = :openid 
                                    order by c.id desc limit ".$limit;
        $list = db_select($sql, array(":openid"=>$openid));
        return $list;
    }


    static function addGoods($openid,$goodsid,$shopid,$agentid,$uniacid) {
        if (empty($openid)) {
            return false;
        }
        db_query("UPDATE ".tablename(t_goods)." SET scansum = scansum +1 WHERE id =:id",array(":id"=>$goodsid));
        $sql = "SELECT * FROM " . tablename(t_scan) . " where openid = :openid and goodsid =:goodsid and type ='goods'";
        $row = db_find($sql, array(":openid"=>$openid,":goodsid"=>$goodsid));
        if(empty($row)){
            $data = array(
                'uniacid'   => $uniacid,
                'goodsid'   => $goodsid,
                'shopid'    => $shopid,
                'agentid'   => $agentid,
                'openid'    => $openid,
                'addtime'   => TIMESTAMP,
                "lasttime"  =>TIMESTAMP,
                'num'       => 1,
                'status'    => 1,
                'type'      => 'goods',
            );
            $insertid = db_insert(t_scan, $data);
            $id = $insertid;
            return false;
        }else{
            db_update(t_scan,array("lasttime"=>TIMESTAMP,"num"=>$row['num']+1),array ('id'=>$row['id']));
            return false;
        }
    }

    static function getNumByThemeIdAndOpenidAndStatus($themeid,$openid,$status=0) {
        if (empty($themeid) || empty($openid)) {
            return false;
        }
        $sql = "select count(*) from (SELECT COUNT(*) FROM ".tablename(t_scan)."
    				WHERE themeid = :themeid and fopenid = :openid and status = :status group by openid) a";
        $row = db_fetchcolumn($sql, array(":themeid"=>$themeid,":openid"=>$openid,":status"=>$status));
        return $row;
    }

    static function getNumByThemeIdAndOpenid($themeid,$openid,$status=0) {
        if (empty($themeid) || empty($openid)) {
            return false;
        }
        $sql = "SELECT COUNT(*) FROM ".tablename(t_scan)."
                    WHERE themeid = :themeid and fopenid = :openid and status =1";
        $row = db_fetchcolumn($sql, array(":themeid"=>$themeid,":openid"=>$openid));
        return $row;
    }

    static function getNumByThemeIdAndOpenidXCX($themeid,$uid,$status=0) {
        if (empty($themeid) || empty($uid)) {
            return false;
        }

        $sql = "SELECT * FROM ".tablename(t_scan)." 
                    WHERE themeid = :themeid and fromuid = :uid and status =1";
        $list = db_select($sql, array(":themeid"=>$themeid,":uid"=>$uid));
        $avatarlist=array();
        foreach ($list as $key => $row) {
            $fans = Fans::getById($row['uid'],$themeid);
            $avatar = array('avatar'=>$fans['avatar']);
            if($status==1) {
                if ($fans['nickname']<>'' || $fans['avatar']<>''){
                    array_push($avatarlist,$avatar);
                }
            }elseif($status==2) {
                if ($fans['mobile']<>''){
                    array_push($avatarlist,$avatar);
                }
            }elseif($status==3) {
                if ($fans['mobile']<>'' && ($fans['nickname']<>'' || $fans['avatar']<>'')){
                    array_push($avatarlist,$avatar);
                }
            }else{
                array_push($avatarlist,$avatar);
            }

        }
        return $avatarlist;
    }
    
    static function addShop($openid,$shopid,$agentid,$uniacid) {
        if (empty($openid)) {
            return false;
        }
        db_query("UPDATE ".tablename(t_shop)." SET scansum = scansum +1 WHERE id =:id",array(":id"=>$shopid));
        $sql = "SELECT * FROM " . tablename(t_scan) . " where openid = :openid and shopid =:shopid and type = 'shop'";
        $row = db_find($sql, array(":openid"=>$openid,":shopid"=>$shopid));
        if(empty($row)){
            $data = array(
                'uniacid'   => $uniacid,
                'shopid'    => $shopid,
                'agentid'   => $agentid,
                'openid'    => $openid,
                'addtime'   => TIMESTAMP,
                "lasttime"  =>TIMESTAMP,
                'num'       => 1,
                'status'    => 1,
                'type'      => 'shop',
            );
            $insertid = db_insert(t_scan, $data);
            $id = $insertid;
            return false;
        }else{
            db_update(t_scan,array("lasttime"=>TIMESTAMP,"num"=>$row['num']+1),array ('id'=>$row['id']));
            return false;
        }
    }

    static function addAgent($openid,$agentid,$uniacid) {
        if (empty($openid)) {
            return false;
        }
        db_query("UPDATE ".tablename(t_agent)." SET scansum = scansum +1 WHERE id =:id",array(":id"=>$agentid));
        $sql = "SELECT * FROM " . tablename(t_scan) . " where openid = :openid and agentid =:agentid and type = 'agent'";
        $row = db_find($sql, array(":openid"=>$openid,":agentid"=>$agentid));
        if(empty($row)){
            $data = array(
                'uniacid'   => $uniacid,
                'agentid'   => $agentid,
                'openid'    => $openid,
                'addtime'   => TIMESTAMP,
                "lasttime"  =>TIMESTAMP,
                'num'       => 1,
                'status'    => 1,
                'type'      => 'agent',
            );
            $insertid = db_insert(t_scan, $data);
            $id = $insertid;
            return false;
        }else{
            db_update(t_scan,array("lasttime"=>TIMESTAMP,"num"=>$row['num']+1),array ('id'=>$row['id']));
            return false;
        }
    }

}
?>